/**
 * <b> 文件: </b> Common.java
 * @author xhe
 * @version V1.0.0.0
 * @version --2020-10-27
 */
package xhe.lib.data;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.sql.ResultSet;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;

import xhe.entities.ApiEntity;
import xhe.lib.base.Common;
import xhe.lib.date.DateTime;
import xhe.lib.db.BaseFactory;

/**
 * <b> 类: </b> Common.java
 * 
 * @author xhe
 * @version V1.0.0.0
 * @version --2020-10-27
 */
public class Util {

	/**
	 * <b> 方法: </b> 获取查询条件
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return String 查询条件的SQL语句
	 * @version V1.0.0.0
	 * @version --2020年5月6日
	 * @version --新增方法
	 * @version V1.0.1.0
	 * @version --2020年8月27日
	 * @version --将Filter对象由原来的键值对象修改对数组对象
	 */
	@Deprecated
	public static String getFilter(String params) {
		String strFilter = "";

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);

			// 解析请求串(json)
			JSONArray jsonFilter = jsonParams.getJSONArray("Filter");

//			System.out.println("FilterSize:" + jsonFilter.size());

			for (int i = 0; i < jsonFilter.size(); i++) {

				JSONArray oFilter = JSONArray.from(jsonFilter.get(i));
				String strKey = oFilter.getString(0).toString();
				String strUnion = oFilter.getString(1).toString();
				String strCalculate = oFilter.getString(2).toString();
				String strType = oFilter.getString(3).toString();
				String strValue = oFilter.getString(4).toString();

//				System.out.println("Key:" + strKey + ",Union:" + strUnion + ",Calculate:" + strCalculate + ",Type:"
//						+ strType + ",Value:" + strValue);

				if (StringUtils.isNotEmpty(strValue) && !strValue.equals("null")) {
					if (strValue.equals("EMPTY")) {
						strValue = "";
					}
					if (strUnion.equals("A")) {
						strUnion = " and ";
					} else {
						strUnion = " or ";
					}

					if (strType.equals("C") || strType.equals("D")) {
						// varchar,date
						if (strCalculate.equals("LI")) {
							strValue = "'%" + strValue + "%'";
						} else if (strCalculate.equals("IN")) {
							if (strValue.contains(",")) {
								String[] aryValues = strValue.split(",");
								String strNewVal = "";
								for (String strVal : aryValues) {
									strNewVal += "'" + strVal + "',";
								}

								strValue = strNewVal.substring(0, strNewVal.length() - 1);
							} else {
								strValue = "'" + strValue + "'";
							}
						} else if ((strCalculate.equals("GR") || strCalculate.equals("GE")) && strType.equals("D")
								&& strValue.length() == 10) {
							strValue = "'" + strValue + " 00:00:00'";
						} else if ((strCalculate.equals("LS") || strCalculate.equals("LE")) && strType.equals("D")
								&& strValue.length() == 10) {
							strValue = "'" + strValue + " 23:59:59'";
						} else {
							strValue = "'" + strValue + "'";
						}
					} else {
						// int,bool
						if (strValue.contains("-") && (strValue.length() == 10 || strValue.length() == 7)) {
							strValue = strValue.replace("-", "");
						}
					}

					if (strCalculate.equals("LI")) {
						strCalculate = " like {0} ";
					} else if (strCalculate.equals("BE")) {
						strCalculate = " between {0} and {1} ";
					} else if (strCalculate.equals("GR")) {
						strCalculate = " > {0}";
					} else if (strCalculate.equals("LS")) {
						strCalculate = " < {0}";
					} else if (strCalculate.equals("GE")) {
						strCalculate = " >= {0}";
					} else if (strCalculate.equals("LE")) {
						strCalculate = " <= {0}";
					} else if (strCalculate.equals("IN")) {
						strCalculate = " in ({0}) ";
					} else if (strCalculate.equals("NI")) {
						strCalculate = " not in ({0}) ";
					} else if (strCalculate.equals("NE")) {
						strCalculate = " <> {0}";
					} else {
						strCalculate = " = {0}";
					}

					strFilter += MessageFormat.format(strUnion + strKey + strCalculate, strValue);

				}
			}
		} catch (Exception ex) {
		}

		return strFilter;
	}

	/**
	 * <b> 方法: </b> 获取查询条件
	 * 
	 * @author xhe
	 * @param filterList 过滤条件列表
	 * @return String 查询条件的SQL语句
	 * @version V1.0.0.0
	 * @version --2020年5月6日
	 * @version --新增方法
	 * @version V1.0.1.0
	 * @version --2020年8月27日
	 * @version --将Filter对象由原来的键值对象修改对数组对象
	 * @version V1.0.2.0
	 * @version --2020年10月30日
	 * @version --基于getFilterList()方法重构
	 */
	public static String getFilter(Map<Integer, List<String>> filterList) {
		String strFilter = "";

		try {

			for (int iIndex : filterList.keySet()) {
				strFilter += MessageFormat.format(
						filterList.get(iIndex).get(2) + filterList.get(iIndex).get(0) + filterList.get(iIndex).get(3),
						filterList.get(iIndex).get(1));
			}

		} catch (Exception ex) {
		}

		return strFilter;
	}

	/**
	 * <b> 方法: </b> 获取查询条件
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return String 查询条件的SQL语句
	 * @version V1.0.0.0
	 * @version --2020年5月6日
	 * @version --新增方法
	 * @version V1.0.1.0
	 * @version --2020年8月27日
	 * @version --将Filter对象由原来的键值对象修改为数组对象
	 * @version V1.1.0.0
	 * @version --2020年10月30日
	 * @version --将Filter对象由原来的键值对象重构为Map对象
	 */
	public static Map<Integer, List<String>> getFilterList(String params) {
		Map<Integer, List<String>> oFilterList = new HashMap<Integer, List<String>>();

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);

			// 解析请求串(json)
			JSONArray jsonFilter = jsonParams.getJSONArray("Filter");

//			System.out.println("FilterSize:" + jsonFilter.size());

			for (int i = 0; i < jsonFilter.size(); i++) {

				JSONArray oFilter = JSONArray.from(jsonFilter.get(i));

				String strKey = oFilter.getString(0).toString();
				String strUnion = oFilter.getString(1).toString();
				String strCalculate = oFilter.getString(2).toString();
				String strType = oFilter.getString(3).toString();
				String strValue = oFilter.getString(4).toString();

//				System.out.println("Key:" + strKey + ",Union:" + strUnion + ",Calculate:" + strCalculate + ",Type:"
//						+ strType + ",Value:" + strValue);

				if (StringUtils.isNotEmpty(strValue)) {
					if (strValue.equals("EMPTY")) {
						strValue = "";
					}
					if (strUnion.equals("A")) {
						strUnion = " and ";
					} else {
						strUnion = " or ";
					}

					if (strType.equals("C") || strType.equals("D")) {
						// varchar,date
						if (strCalculate.equals("LI")) {
							strValue = "'%" + strValue + "%'";
						} else if (strCalculate.equals("IN")) {
							if (strValue.contains(",")) {
								String[] aryValues = strValue.split(",");
								String strNewVal = "";
								for (String strVal : aryValues) {
									strNewVal += "'" + strVal + "',";
								}

								strValue = strNewVal.substring(0, strNewVal.length() - 1);
							} else {
								strValue = "'" + strValue + "'";
							}
						} else if ((strCalculate.equals("GR") || strCalculate.equals("GE")) && strType.equals("D")
								&& strValue.length() == 10) {
							strValue = "'" + strValue + " 00:00:00'";
						} else if ((strCalculate.equals("LS") || strCalculate.equals("LE")) && strType.equals("D")
								&& strValue.length() == 10) {
							strValue = "'" + strValue + " 23:59:59'";
						} else {
							strValue = "'" + strValue + "'";
						}
					} else {
						// int,bool
						if (strValue.contains("-") && (strValue.length() == 10 || strValue.length() == 7)) {
							strValue = strValue.replace("-", "");
						}
					}

					if (strCalculate.equals("LI")) {
						strCalculate = " like {0} ";
					} else if (strCalculate.equals("BE")) {
						strCalculate = " between {0} and {1} ";
					} else if (strCalculate.equals("GR")) {
						strCalculate = " > {0}";
					} else if (strCalculate.equals("LS")) {
						strCalculate = " < {0}";
					} else if (strCalculate.equals("GE")) {
						strCalculate = " >= {0}";
					} else if (strCalculate.equals("LE")) {
						strCalculate = " <= {0}";
					} else if (strCalculate.equals("IN")) {
						strCalculate = " in ({0}) ";
					} else if (strCalculate.equals("NI")) {
						strCalculate = " not in ({0}) ";
					} else if (strCalculate.equals("NE")) {
						strCalculate = " <> {0}";
					} else {
						strCalculate = " = {0}";
					}

					List<String> oFilterEntity = new ArrayList<String>();
					oFilterEntity.add(strKey);
					oFilterEntity.add(strValue);
					oFilterEntity.add(strUnion);
					oFilterEntity.add(strCalculate);

					oFilterList.put(i, oFilterEntity);

				}
			}
		} catch (Exception ex) {
		}

		return oFilterList;
	}

	/**
	 * <b> 方法: </b> 获取查询列
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return String 列名,以','分割
	 * @version V1.0.0.0
	 * @version --2020年5月6日
	 * @version --新增方法
	 */
	public static String getColumns(String params) {
		String strColumns = "";

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);

			// 解析请求串(json)
			JSONArray jsonColumns = jsonParams.getJSONArray("Columns");

			for (int i = 0; i < jsonColumns.size(); i++) {

				JSONArray oColumn = JSONArray.from(jsonColumns.get(i));

				if (StringUtils.isNotEmpty(oColumn.getString(0).toString())) {

					if (Common.toPraseBoolean(oColumn.getString(6).toString(), true)) {
						strColumns += oColumn.getString(0).toString() + ",";
					}
				}
			}

			if (strColumns != "") {
				strColumns = strColumns.substring(0, strColumns.length() - 1);
			}

		} catch (Exception ex) {
		}

		return strColumns;
	}

	/**
	 * <b> 方法: </b> 获取查询列
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return String 列名,以','分割
	 * @version V1.0.0.0
	 * @version --2020年9月8日
	 * @version --新增方法
	 */
	public static String getColumns_2_0(String params) {
		String strColumns = "";

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);

			// 解析请求串(json)
			JSONArray jsonColumns = jsonParams.getJSONArray("Columns");

			for (int i = 0; i < jsonColumns.size(); i++) {

				JSONArray oColumn = JSONArray.from(jsonColumns.get(i));

				if (StringUtils.isNotEmpty(oColumn.getString(0).toString())) {

					if (Common.toPraseBoolean(oColumn.getString(6).toString(), true)) {

						String strColumn = oColumn.getString(0).toString();
						String strName = oColumn.getString(1).toString();

						strColumns += strColumn + " '" + strName + "',";

					}
				}
			}

			if (strColumns != "") {
				strColumns = strColumns.substring(0, strColumns.length() - 1);
			}

		} catch (Exception ex) {
		}

		return strColumns;
	}

	/**
	 * <b> 方法: </b> 获取查询列
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @param type   数据库类型
	 * @return String 列名,以','分割
	 * @version V1.0.0.0
	 * @version --2023年5月5日
	 * @version --新增方法
	 */
	public static String getColumns_2_0(String params, String type) {
		String strColumns = "";

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);

			// 解析请求串(json)
			JSONArray jsonColumns = jsonParams.getJSONArray("Columns");

			for (int i = 0; i < jsonColumns.size(); i++) {

				JSONArray oColumn = JSONArray.from(jsonColumns.get(i));

				if (StringUtils.isNotEmpty(oColumn.getString(0).toString())) {

					if (Common.toPraseBoolean(oColumn.getString(6).toString(), true)) {

						String strColumn = oColumn.getString(0).toString();
						String strName = oColumn.getString(1).toString();

						if (type.contains("gauss")) {
							strColumns += strColumn + " " + strName + ",";
						} else {
							strColumns += strColumn + " '" + strName + "',";
						}

					}
				}
			}

			if (strColumns != "") {
				strColumns = strColumns.substring(0, strColumns.length() - 1);
			}

		} catch (Exception ex) {
		}

		return strColumns;
	}

	/**
	 * <b> 方法: </b> 获取查询列
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return String 列名,以','分割
	 * @version V1.0.0.0
	 * @version --2021年5月20日
	 * @version --新增方法
	 */
	public static String getColumns_3_0(String params) {
		String strColumns = "";

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);

			// 解析请求串(json)
			JSONArray jsonColumns = jsonParams.getJSONArray("Columns");

			for (int i = 0; i < jsonColumns.size(); i++) {

				JSONArray oColumn = JSONArray.from(jsonColumns.get(i));

				if (StringUtils.isNotEmpty(oColumn.getString(0).toString())) {

					strColumns += oColumn.getString(0).toString() + ",";

				}
			}

			if (strColumns != "") {
				strColumns = strColumns.substring(0, strColumns.length() - 1);
			}

		} catch (Exception ex) {
		}

		return strColumns;
	}

	/**
	 * <b> 方法: </b> 获取查询列及属性
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return Map 列及属性的对象集合
	 * @version V1.0.0.0
	 * @version --2020年5月6日
	 * @version --新增方法
	 */
	public static Map<String, List<String>> getColumnList(String params) {

		Map<String, List<String>> oColumnList = new HashMap<String, List<String>>();

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);
			// 解析请求串(json)
			JSONArray jsonColumns = jsonParams.getJSONArray("Columns");

			for (int i = 0; i < jsonColumns.size(); i++) {

				JSONArray oColumn = JSONArray.from(jsonColumns.get(i));

				if (StringUtils.isNotEmpty(oColumn.getString(0).toString())) {
					// 获取Value
					String strKey = oColumn.getString(0).toString();
					String strValue = oColumn.getString(1).toString();
					// String strWidth = oColumn.getString(2).toString();
					String strDefault = oColumn.getString(3).toString();
					String strType = oColumn.getString(4).toString();
					String strFormat = oColumn.getString(5).toString();
					Boolean blBind = Common.toPraseBoolean(oColumn.getString(6).toString(), true);

					/*
					 * System.out.println("Key:" + strKey + ",Value:" + strValue + ",Type:" +
					 * strType + ",Format:" + strFormat + ",Bind:" + blBind);
					 */

					if (strValue.contains("\"[") && strValue.contains("]\"")) {
						strValue = strValue.replace("\"[", "[").replace("]\"", "]");
					}

					if (blBind) {
						List<String> oAttributeList = new ArrayList<String>();
						oAttributeList.add(strValue);
						oAttributeList.add(strType);
						oAttributeList.add(strFormat);
						oAttributeList.add(strDefault);

						oColumnList.put(strKey.toString(), oAttributeList);
					}
				}
			}

		} catch (Exception ex) {
		}

		return oColumnList;
	}

	/**
	 * <b> 方法: </b> 获取查询列及属性
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return Map 列及属性的对象集合
	 * @version V1.0.0.0
	 * @version --2020年5月6日
	 * @version --新增方法
	 */
	public static Map<String, List<String>> getColumnList_2_0(String params) {

		Map<String, List<String>> oColumnList = new HashMap<String, List<String>>();

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);
			// 解析请求串(json)
			JSONArray jsonColumns = jsonParams.getJSONArray("Columns");

			for (int i = 0; i < jsonColumns.size(); i++) {

				JSONArray oColumn = JSONArray.from(jsonColumns.get(i));

				if (StringUtils.isNotEmpty(oColumn.getString(0).toString())) {
					// 获取Value
					String strKey = oColumn.getString(1).toString();
					String strColumn = oColumn.getString(0).toString();
					// String strWidth = oColumn.getString(2).toString();
					String strDefault = oColumn.getString(3).toString();
					String strType = oColumn.getString(4).toString();
					String strFormat = oColumn.getString(5).toString();
					Boolean blBind = Common.toPraseBoolean(oColumn.getString(6).toString(), true);

					/*
					 * System.out.println("Key:" + strKey + ",Value:" + strValue + ",Type:" +
					 * strType + ",Format:" + strFormat + ",Bind:" + blBind);
					 */

					if (blBind) {
						List<String> oAttributeList = new ArrayList<String>();
						oAttributeList.add(strColumn);
						oAttributeList.add(strType);
						oAttributeList.add(strFormat);
						oAttributeList.add(strDefault);

						oColumnList.put(strKey.toString(), oAttributeList);
					}
				}
			}

		} catch (Exception ex) {
		}

		return oColumnList;
	}

	/**
	 * <b> 方法: </b> 获取查询列及属性
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return Map 列及属性的对象集合
	 * @version V1.0.0.0
	 * @version --2020年5月6日
	 * @version --新增方法
	 */
	public static Map<String, List<String>> getColumnList_3_0(String params) {

		Map<String, List<String>> oColumnList = new HashMap<String, List<String>>();

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);
			// 解析请求串(json)
			JSONArray jsonColumns = jsonParams.getJSONArray("Columns");

			for (int i = 0; i < jsonColumns.size(); i++) {

				JSONArray oColumn = JSONArray.from(jsonColumns.get(i));

				if (StringUtils.isNotEmpty(oColumn.getString(0).toString())) {
					// 获取Value
					String strColumn = oColumn.getString(7).toString();
					String strKey = oColumn.getString(0).toString();
					// String strName = oColumn.getString(1).toString();
					// String strWidth = oColumn.getString(2).toString();
					String strDefault = oColumn.getString(3).toString();
					String strType = oColumn.getString(4).toString();
					String strFormat = oColumn.getString(5).toString();
					Boolean blBind = Common.toPraseBoolean(oColumn.getString(6).toString(), true);

					/*
					 * System.out.println("Key:" + strKey + ",Value:" + strValue + ",Type:" +
					 * strType + ",Format:" + strFormat + ",Bind:" + blBind);
					 */

					if (blBind) {
						List<String> oAttributeList = new ArrayList<String>();
						oAttributeList.add(strColumn);
						oAttributeList.add(strType);
						oAttributeList.add(strFormat);
						oAttributeList.add(strDefault);

						oColumnList.put(strKey.toString(), oAttributeList);
					}
				}
			}

		} catch (Exception ex) {
		}

		return oColumnList;
	}

	/**
	 * <b> 方法: </b> 获取查询列及属性
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return Map 列及属性的对象集合
	 * @version V1.0.0.0
	 * @version --2021年5月20日
	 * @version --新增方法
	 */
	public static Map<String, List<String>> getColumnList_4_0(String params) {

		Map<String, List<String>> oColumnList = new LinkedHashMap<String, List<String>>();

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);
			// 解析请求串(json)
			JSONArray jsonColumns = jsonParams.getJSONArray("Columns");

			for (int i = 0; i < jsonColumns.size(); i++) {

				JSONArray oColumn = JSONArray.from(jsonColumns.get(i));

				if (StringUtils.isNotEmpty(oColumn.getString(0).toString())) {
					// 获取Value
					String strColumn = oColumn.getString(0).toString();
					String strName = oColumn.getString(1).toString();
					String strType = oColumn.getString(2).toString();
					String strFormat = oColumn.getString(3).toString();

//					System.out.println(
//							"Column:" + strColumn + ",Name:" + strName + ",Type:" + strType + ",Format:" + strFormat);

					List<String> oAttributeList = new ArrayList<String>();
					oAttributeList.add(strColumn);
					oAttributeList.add(strName);
					oAttributeList.add(strType);
					oAttributeList.add(strFormat);

					oColumnList.put(strName.toString(), oAttributeList);
				}
			}

		} catch (Exception ex) {
		}

		return oColumnList;
	}

	/**
	 * <b> 方法: </b> 获取连接关联语句
	 * 
	 * @author xhe
	 * @param params 接口传参
	 * @return String join语句
	 * @version V1.0.0.0
	 * @version --2020年9月22日
	 * @version --新增方法
	 */
	public static String getJoinUnion(String params) {

		String strJoinUnion = "";

		try {
			// 遍历获取Key,Value
			JSONObject jsonParams = JSON.parseObject(params);

			// 解析请求串(json)
			JSONArray jsonJoins = jsonParams.getJSONArray("Join");

			// System.out.println("JoinSize:" + jsonJoins.size());

			for (int i = 0; i < jsonJoins.size(); i++) {

				JSONObject oJoin = JSONObject.from(jsonJoins.get(i));

				// 获取Value
				String strTable = oJoin.getString("Table").toString();
				String strJoin = oJoin.getString("Union").toString();
				JSONArray jsonFilter = oJoin.getJSONArray("Filter");
				String strFilter = "";

				// System.out.println("JoinTable:" + strTable);
				// System.out.println("JoinUnion:" + strJoin);
				// System.out.println("JoinFilterSize:" + jsonFilter.size());

				for (int j = 0; j < jsonFilter.size(); j++) {

					JSONArray oFilter = JSONArray.from(jsonFilter.get(j));
					String strColumn = oFilter.getString(0).toString();
					String strUnion = oFilter.getString(1).toString();
					String strCalculate = oFilter.getString(2).toString();
					String strType = oFilter.getString(3).toString();
					String strValue = oFilter.getString(4).toString();

					// System.out.println("JoinColumn:" + strColumn);
					// System.out.println("JoinUnion:" + strUnion);
					// System.out.println("JoinCalculate:" + strCalculate);
					// System.out.println("JoinType:" + strType);
					// System.out.println("JoinValue:" + strValue);

					if (strUnion.equals("A")) {
						strUnion = " and ";
					} else {
						strUnion = " or ";
					}

					if (!strValue.contains(".")) {
						if (strType.equals("C") || strType.equals("D")) {
							// varchar,date
							if (strCalculate.equals("LI")) {
								strValue = "'%" + strValue + "%'";
							} else if (strCalculate.equals("IN")) {
								if (strValue.contains(",")) {
									String[] aryValues = strValue.split(",");
									String strNewVal = "";
									for (String strVal : aryValues) {
										strNewVal += "'" + strVal + "',";
									}

									strValue = strNewVal.substring(0, strNewVal.length() - 1);
								} else {
									strValue = "'" + strValue + "'";
								}
							} else if ((strCalculate.equals("GR") || strCalculate.equals("GE")) && strType.equals("D")
									&& strValue.length() == 10) {
								strValue = "'" + strValue + " 00:00:00'";
							} else if ((strCalculate.equals("LS") || strCalculate.equals("LE")) && strType.equals("D")
									&& strValue.length() == 10) {
								strValue = "'" + strValue + " 23:59:59'";
							} else {
								strValue = "'" + strValue + "'";
							}
						} else {
							// int,bool
							if (strValue.contains("-") && (strValue.length() == 10 || strValue.length() == 7)) {
								strValue = strValue.replace("-", "");
							}
						}

					}

					if (strCalculate.equals("LI")) {
						strCalculate = " {0} like {1} ";
					} else if (strCalculate.equals("BE")) {
						// strCalculate = " between {0} and {1} ";
					} else if (strCalculate.equals("GR")) {
						strCalculate = " {0} > {1}";
					} else if (strCalculate.equals("LS")) {
						strCalculate = " {0} < {1}";
					} else if (strCalculate.equals("GE")) {
						strCalculate = " {0} >= {1}";
					} else if (strCalculate.equals("LE")) {
						strCalculate = " {0} <= {1}";
					} else if (strCalculate.equals("IN")) {
						strCalculate = " {0} in ({1}) ";
					} else if (strCalculate.equals("NI")) {
						strCalculate = " {0} not in ({1}) ";
					} else if (strCalculate.equals("NE")) {
						strCalculate = " {0} <> {1}";
					} else {
						strCalculate = " {0} = {1}";
					}

					if (j == 0) {
						strFilter += MessageFormat.format(strCalculate, strColumn, strValue);
					} else {
						strFilter += MessageFormat.format(strUnion + strCalculate, strColumn, strValue);
					}

					// System.out.println("JoinFilter:" + strFilter);
				}

				strJoinUnion += " " + strJoin + " join " + strTable + " on " + strFilter;
			}

			// System.out.println("JoinUnion:" + strJoinUnion);

		} catch (Exception ex) {
		}

		return strJoinUnion;
	}

	/**
	 * <b> 方法: </b> 获取请求串
	 * 
	 * @author xhe
	 * @param inputStream 接口传参
	 * @return String 请求串
	 * @version V1.0.0.0
	 * @version --2019年12月1日
	 * @version --新增方法
	 */
	public static String getRequest(InputStream inputStream) {

		String strRequest = "";
		try {

			// 解析请求串
			StringBuffer sbRequest = new StringBuffer();
			InputStreamReader oInputStreamReader = new InputStreamReader(inputStream);
			BufferedReader oBufferedReader = new BufferedReader(oInputStreamReader);
			String strLine = "";

			// 循环
			while ((strLine = oBufferedReader.readLine()) != null) {
				sbRequest.append(strLine);
			}
			// Url解码
			strRequest = java.net.URLDecoder.decode(sbRequest.toString(), "UTF-8");

		} catch (IOException e) {
			e.printStackTrace();
		}

		return strRequest;
	}

	/**
	 * <b> 方法: </b> 获取表最大的主键值
	 * 
	 * @author xhe
	 * @param tableName 表名
	 * @param key       表主键的Key
	 * @return String 最大的主键值
	 * @version V1.0.0.0
	 * @version --2020年8月5日
	 * @version --新增方法
	 */
	public static String getMaxPrimaryKey(ApiEntity requestEntity, String tableName, String columnName, String key) {
		String strMaxPrimaryKey = "";

		try {

			BaseFactory oBaseFactory = new BaseFactory(requestEntity);

			String strSql = "select max(" + columnName + ") maxKey from " + tableName + " where " + columnName
					+ " like '" + key + "%'";

			System.out.println("MaxSql:" + strSql);

			// 查询数据
			ResultSet rs = oBaseFactory.Query(strSql, requestEntity);

			// 封装返回对象
			while (rs.next()) {

				if (rs.getString("maxKey") != null && !rs.getString("maxKey").toLowerCase().equals("null")) {
					int iKey = Common.toPraseInt(rs.getString("maxKey").replace(key, ""), 0) + 1;

					strMaxPrimaryKey = key + String.format("%04d", iKey);
				}
			}

		} catch (Exception e) {
			e.printStackTrace();
		}

		if (strMaxPrimaryKey.equals("")) {
			strMaxPrimaryKey = key + "0001";
		}

		return strMaxPrimaryKey;
	}

	/**
	 * <b> 方法: </b> 处理sql语句中的特殊符号
	 * 
	 * @author xhe
	 * @param sqlText sql语句
	 * @return String 处理后的sql语句
	 * @version V1.0.0.0
	 * @version --2020年11月20日
	 * @version --新增方法
	 */
	public static String decodeSpecialSymbol(String sqlText) {
		try {
			// 单引号是oracle字符串的边界,oralce中用2个单引号代表1个单引号
			String afterDecode = sqlText.replaceAll("'", "''");
			// 由于使用了/作为ESCAPE的转义特殊字符,所以需要对该字符进行转义
			// 这里的作用是将"a/a"转成"a//a"
			afterDecode = afterDecode.replaceAll("/", "//");
			// 使用转义字符 /,对oracle特殊字符% 进行转义,只作为普通查询字符，不是模糊匹配
			afterDecode = afterDecode.replaceAll("%", "/%");
			// 使用转义字符 /,对oracle特殊字符_ 进行转义,只作为普通查询字符，不是模糊匹配
			afterDecode = afterDecode.replaceAll("_", "/_");
			return afterDecode;
		} catch (Exception ex) {
			return sqlText;
		}
	}

	/**
	 * <b> 方法: </b> 解析链接字符串
	 * 
	 * @author xhe
	 * @param Connection 链接字符串
	 * @return Map<String, Map<String, String>> 数据库链接对象
	 * @version V1.0.0.0
	 * @version --2021年05月20日
	 * @version --新增方法
	 */
	public static Map<String, Map<String, String>> getConnection(String Connection) {
		try {
			Map<String, Map<String, String>> oDataSourceMap = new HashMap<String, Map<String, String>>();

			String[] aryConnection = Connection.split("##");

			Map<String, String> oConnection = new HashMap<String, String>();
			oConnection.put("jdbc-url", aryConnection[0]);
			oConnection.put("username", aryConnection[1]);
			oConnection.put("password", aryConnection[2]);

			if (aryConnection[0].contains("mysql")) {
				oConnection.put("driver-class-name", "org.gjt.mm.mysql.Driver");
			} else if (aryConnection[0].contains("gaussdb")) {
				oConnection.put("driver-class-name", "com.huawei.gauss200.jdbc.Driver");
			} else if (aryConnection[0].contains("sqlserver")) {
				oConnection.put("driver-class-name", "com.microsoft.sqlserver.jdbc.SQLServerDriver");
			}

			oDataSourceMap.put("default", oConnection);

			return oDataSourceMap;
		} catch (Exception ex) {
			return null;
		}
	}

	/**
	 * <b> 方法: </b> 解析字段
	 * 
	 * @author xhe
	 * @param Connection 链接字符串
	 * @return Map<String, Map<String, String>> 数据库链接对象
	 * @version V1.0.0.0
	 * @version --2021年05月20日
	 * @version --新增方法
	 */
	public static String analysisColumn(String column, String type, String format) {

		Object strValue = "";

		try {

			if (type.equals("D")) {

				// 日期类型
				strValue = "\"" + DateTime.toString(column, format) + "\"";

			} else if (type.equals("F")) {
				// 浮点类型
				strValue = Common.toPraseDecimal(column, 0);

				if (StringUtils.isNotEmpty(format)) {
					strValue = Common.getNumber(strValue.toString(), Common.toPraseInt(format, 2));
				}

			} else if (type.equals("I")) {
				// 数字类型
				strValue = Common.toPraseLong(column, 0);

			} else if (type.equals("A")) {
				// 数组类型
				strValue = Common.isNullEmpty(column, "");

				if (StringUtils.isEmpty(strValue.toString())) {

					strValue = "[]";
				}

			} else {
				// 其他类型
				strValue = Common.isNullEmpty(column, "");

				if (StringUtils.isNotEmpty(format)) {

					strValue = Common.toFormat(strValue.toString(), format);

				}
				// System.out.println(strValue.toString());

				strValue = "\"" + strValue + "\"";

				// System.out.println(strRow.toString());
			}

			return strValue.toString().trim();

		} catch (Exception ex) {
			return "\"" + column + "\"";
		}
	}

	/**
	 * <b> 方法: </b> 解析字段
	 * 
	 * @author xhe
	 * @param Connection 链接字符串
	 * @return Map<String, Map<String, String>> 数据库链接对象
	 * @version V1.0.0.0
	 * @version --2021年05月20日
	 * @version --新增方法
	 */
	public static String analysisColumn_2_0(String column, String type, String format) {

		Object strValue = "";

		try {

			if (type.equals("D")) {

				// 日期类型
				strValue = DateTime.toString(column, format);

			} else if (type.equals("F")) {
				// 浮点类型
				strValue = Common.toPraseDouble(column, 0);

				if (StringUtils.isNotEmpty(format)) {
					strValue = Common.getNumber(strValue.toString(), Common.toPraseInt(format, 2));
				}

			} else if (type.equals("I")) {
				// 数字类型
				strValue = Common.toPraseInt(column, 0);

			} else if (type.equals("A")) {
				// 数组类型
				strValue = Common.isNullEmpty(column, "");

				if (StringUtils.isEmpty(strValue.toString())) {

					strValue = "[]";
				}

			} else {
				// 其他类型
				strValue = Common.isNullEmpty(column, "");

				if (StringUtils.isNotEmpty(format)) {

					if (format.contains("{0}")) {
						strValue = format.replace("{0}", strValue.toString());
					}

					strValue = Common.toFormat(strValue.toString(), format);

				}

			}

			return strValue.toString();

		} catch (Exception ex) {
			return column;
		}
	}
}
